﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SprintBoardMaker.Data;
using SprintBoardMaker.Views;
using TrelloNet;

namespace SprintBoardMaker
{
    class MainViewModel
    {
        private string _authToken = "";
        public string AuthToken
        {
            get { return _authToken; }
            set
            {
                _authToken = value;
                SaveAuthTokenCmd.RaiseCanExecuteChanged();
            }
        }

        public ITrelloAccess TrelloAccess { get; set; }
        public DelegateCommand OpenAuthFormCmd { get; set; }
        public DelegateCommand SaveAuthTokenCmd { get; set; }

        private NormalizedBoard _selectedBoard;
        public NormalizedBoard SelectedBoard
        {
            get { return _selectedBoard; }
            set
            {
                _selectedBoard = value;
            }
        }

        public MainViewModel()
        {
            TrelloAccess = TrelloAccessFactory.GetTrelloInterface();
            OpenAuthFormCmd = new DelegateCommand(OpenAuthForm);
            SaveAuthTokenCmd = new DelegateCommand(SaveAuthToken, CanSaveAuthToken);

            AuthToken = "";
        }

        private void OpenAuthForm(object o)
        {
            new AuthorizationPageView(this.TrelloAccess.GetAuthorizationURI()).ShowDialog();
        }

        private bool CanSaveAuthToken(object o)
        {
            return !String.IsNullOrEmpty(AuthToken);
        }

        private void SaveAuthToken(object o)
        {
            TrelloAccess.SaveAuthKey(AuthToken);
            return;
        }
    }
}
